package com.atguigu.edu.publisher.controller;

import com.atguigu.edu.publisher.beans.TradeSubjectOrderAmount;
import com.atguigu.edu.publisher.service.TradeStatsService;
import com.atguigu.edu.publisher.utils.DateFormatUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 交易域统计Controller
 */
@RestController
public class TradeStatsController {

    @Autowired
    private TradeStatsService tradeStatsService;

    @RequestMapping("/edu")
    public String getGMV(@RequestParam(value = "date",defaultValue = "0") Integer date){
        if(date.equals("0")){
            //如果没有传递日期参数，将当天日期作为查询日期
            date = DateFormatUtil.now();
        }
        BigDecimal gmv= tradeStatsService.getGMV(date);
        String json ="{ \"status\": 0,\"data\": "+gmv+"}";
        return json;
    }


    @RequestMapping("/subject")
    public String getSubjectAmount(
            @RequestParam(value = "date",defaultValue = "0") Integer date,
            @RequestParam(value = "limit",defaultValue = "10") Integer limit){
        if(date.equals("0")){
            date = DateFormatUtil.now();
        }

        List<TradeSubjectOrderAmount> subjectAmountList = tradeStatsService.getSubjectAmount(date,limit);
        List subjectList = new ArrayList();
        List amountList = new ArrayList();

        for (TradeSubjectOrderAmount subjectAmount : subjectAmountList) {
            subjectList.add(subjectAmount.getCourseName());
            amountList.add(subjectAmount.getOrderTotalAmount());
        }


        String json ="{\"status\": 0,\"data\": {\"categories\": [\""+  StringUtils.join(subjectList,"\",\"")+"\"],\n" +
                "\"series\": [{\"name\": \"手机品牌\",\"data\": ["+StringUtils.join(amountList,",")+"]}]}}";
        return json;
    }


}
